package com.gitee.wsl.jvm.okhttp.intercept.transform

import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response

// a request interceptor
typealias RequestProcessor = (OkHttpClient, Request.Builder) -> Request.Builder

// a response interceptor
typealias ResponseProcessor = (Response) -> Unit

/**
 *
 * @FileName:
 *          cn.netdiscovery.http.core.aop.ProcessorStore
 * @author: Tony Shen
 * @date: 2020-10-03 13:34
 * @version: V1.0 <描述当前版本功能>
 */
interface ProcessorStore {

    fun addRequestProcessor(requestProcessor: RequestProcessor)

    fun removeRequestProcessor(requestProcessor: RequestProcessor)

    fun getRequestProcessors(): List<RequestProcessor>

    fun addResponseProcessor(responseProcessor: ResponseProcessor)

    fun removeResponseProcessor(responseProcessor: ResponseProcessor)

    fun getResponseProcessors(): List<ResponseProcessor>
}

/**
 *
 * @FileName:
 *          cn.netdiscovery.http.core.aop.DefaultProcessorStore
 * @author: Tony Shen
 * @date: 2020-10-03 13:48
 * @version: V1.0 <描述当前版本功能>
 */
class DefaultProcessorStore(private val requestProcessors: MutableList<RequestProcessor>,
                            private val responseProcessors: MutableList<ResponseProcessor>) :
    ProcessorStore {

    override fun addRequestProcessor(requestProcessor: RequestProcessor) {
        requestProcessors.add(requestProcessor)
    }

    override fun removeRequestProcessor(requestProcessor: RequestProcessor) {
        requestProcessors.remove(requestProcessor)
    }

    override fun getRequestProcessors(): List<RequestProcessor> = requestProcessors

    override fun addResponseProcessor(responseProcessor: ResponseProcessor) {
        responseProcessors.add(responseProcessor)
    }

    override fun removeResponseProcessor(responseProcessor: ResponseProcessor) {
        responseProcessors.remove(responseProcessor)
    }

    override fun getResponseProcessors(): List<ResponseProcessor> = responseProcessors
}